package top.chukongxiang.project.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;

import java.util.Collection;
import java.util.List;

/**
 * @author ckx
 */
public class FastJsonUtil {

    public static String bean2Json(Object obj) {
        return JSON.toJSONString(obj);
    }

    public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
        return JSON.parseObject(jsonStr, objClass);
    }

    public static <T> List<T> json2List(String jsonStr, Class<T> objClass) {
        return JSON.parseArray(jsonStr, objClass);
    }

    public static String list2Json(Collection<?> list) {
        return JSONArray.toJSONString(list);
    }

    public static <T> List<T> list2List(Collection<?> list, Class<T> objClass) {
        return json2List(list2Json(list), objClass);
    }

    public static <T> T bean2Bean(Object obj, Class<T> dstClass) {
        return json2Bean(bean2Json(obj), dstClass);
    }

    public static boolean isJson(String json) {
        return JSONValidator.from(json).validate();
    }

    public static JSONObject bean2JsonObject(Object o) {
        return (JSONObject) JSONObject.toJSON(o);
    }
}

